//-----------------------------------------------
// Silk Div Stacks (SDS)-
//
// SDS is a rather simple object that represents
// a rather simple idea but creates a rather
// powerful paradigm for AJAX apps.  the 
// SilkDivStack is given a list of divs to
// maintain for an AJAX enabled page.  You can
// initialize an SDS with a comma separated list
// of div id's and it will initialize itself to
// and save the current content of each div.
// 
// You can update the SDS at any time to save 
// state and you can restore to a previous
// state if needed. There is also a pair of nice 
// convenience function to use that allow AJAX updates
// to a dynamic or preexisting SDS in both url/args and
// form submission formats that allow multiple div updates 
// from one AJAX call.
//
//  Prerequisites:
//		- silkajax.js (for fireAJAXRequest)
//      - silknodes.js (for filterText)

//-------------------------------------------------------
// SDS version of the remote update
function remoteUpdateSDS(url,args,elements) {
 
	// create from string or use prebuilt SDS
	sds = null;
	if (typeof elements == 'string') 
		sds = SilkDivStack(elements);
	else
		sds = elements;
	
	// send off to AJAX machinery
	fireAJAXRequest(url,args,updateDivStacks,sds);
	
	// send back the goodies
	return sds;
}

//---------------------------------------------------------
// Remote form submission ala SDS
function remoteFormUpdateSDS(form,elements) { 
	return remoteUpdateSDS(form.action,formData2Args(form),elements);
}

//--------------------------------------------------------
// UI Update callback for remote SDS updates
function updateDivStacks(results,sds) {
	// parse results
	rv = getRemoteResponseResults(results);
	
	// if we're all good (and text based)
	if(rv.code == 1)  
		// slam one in and pop it back out
		sds.pushpop(rv.data);
		
}

//-------------------------------------------------
// The actual SilkDivStack object...
function SilkDivStack(divs) {
	// set up attributes
	this._divs = divs.split(",");
	this._stacks = new Array();
	this.index = 0;
	this.name = this._divs[0];
	
	//----------------------------------------
	// SDS public methods
	
	// extract the data from input
	// and push out to page
	this.pushpop = function (html) {
		// parse and save to stacks
		this.index++;
		for(d=0; d<this._divs.length; d++) 
			this._stacks[d][this.index] = filterText(html,this._divs[d]);

			
		// push out to the page
		this._restore(this.index);
	}
	
	// save the content of each div
	this.push = function () {
		// bump the index
		this.index ++;		
		this._save();

		return this.index;
	}
	
	// restore content from previous step
	this.pop = function () {
		this._restore(this.index);
		if (this.index > 0) 
			this.index --;
			
		return this.index;
	}
	
	// 'ctrl-z' -- Go back to last marker
	// and loose non-saved changes
	this.refresh = function(divId) {
		for(d=0;d<this._divs.length;d++) 
			if (!divId || (divId = this._div[d])) {
				div.innerHTML = this._stacks[d][this.index];
			}	
	}
	
	//----------------------------------------
	// SDS private methods
	
	//  set the values from page to stacks
	this._save = function () {
		for(d=0; d < this._divs.length; d++) {
			div = document.getElementById(this._divs[d]);
			this._stacks[d][this.index] = div.innerHTML;
		}
	}	
	
	// restore values from stacks to page
	this._restore = function (ndx) {
		for(d=0; d < this._divs.length; d++) {
			div = document.getElementById(this._divs[d]);
			div.innerHTML = this._stacks[d][ndx];
		}
	}
	
	//-----------------------------------------
	// intialize the stacks
	
	// initialize div stacks
	for(d=0; d < this._divs.length; d++) {
		this._stacks[d] = new Array();
	}
	
	// save initial content into slot 0
	this._save();

	return this;		
}


